package com.pig4cloud.pigx.wallet.configuration.thread;

import com.pig4cloud.pigx.common.customize.thread.handler.CommonRejectedExecutionHandler;
import com.pig4cloud.pigx.wallet.configuration.wallet.WalletScheduledConfig;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * 线程池配置
 *
 * @since 2022/9/27 19:31
 */
@Configuration
@AllArgsConstructor
public class ThreadPoolConfig {

	private final WalletScheduledConfig walletScheduledConfig;

	/**
	 * 定时任务线程池
	 *
	 * @return {@link TaskScheduler}
	 * @since 2022/9/27 20:34
	 */
	@Bean
	public TaskScheduler taskScheduler() {
		ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
		executor.setPoolSize(walletScheduledConfig.getCorePoolSize());
		executor.setThreadNamePrefix(walletScheduledConfig.getThreadNamePrefix());
		executor.setRejectedExecutionHandler(new CommonRejectedExecutionHandler());
		executor.initialize();
		return executor;
	}
}
